import {_decorator, Component, Node, Label, find, Button} from 'cc';

const {ccclass, property} = _decorator;

@ccclass('Modal')
export class Modal extends Component {

    private static _instance: Modal = null;

    @property(Label)
    title: Label = null;


    @property(Label)
    desc: Label = null;

    @property(Button)
    ConfirmBtn: Button = null!;

    @property(Button)
    CancelBtn: Button = null!;


    public static get Instance() {
        // 只能通过自身进行初始化
        if (this._instance == null) {
            this._instance = find("Canvas/Modal").getComponent(Modal);
        }
        return this._instance;
    }

    start() {

    }

    update(deltaTime: number) {

    }

    public show(title: string, desc: string, confirmCal?: () => void) {
        console.log(this.node)
        this.title.string = title;
        this.desc.string = desc;
        this.node.active = true;
        if (confirmCal) {
            this.ConfirmBtn.node.active = true;
            this.CancelBtn.node.active = true;
            this.ConfirmBtn.node.on(Button.EventType.CLICK, () => {
                this.close();
                confirmCal();
            });
            this.CancelBtn.node.on(Button.EventType.CLICK, () => {
                this.close();
            });
        } else {
            this.ConfirmBtn.node.active = false;
            this.CancelBtn.node.active = false;
        }
    }

    private close() {
        this.node.active = false;
    }
}


